package com.ddzh.security;

import com.aliyuncs.utils.AuthUtils;
import com.ddzh.entity.Result;
import com.ddzh.service.AuthMenuTreeVoService;
import com.ddzh.service.UserService;
import com.ddzh.utils.RedisUtil;
import com.ddzh.vo.AuthMenuTreeVo;
import com.ddzh.web.AuthMenuTreeVoController;
import org.apache.dubbo.config.annotation.Reference;
import org.omg.CORBA.PERSIST_STORE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;

@Component
public class MyUserDetailService implements UserDetailsService {

    @Resource
    private PasswordEncoder passwordEncoder;

    private Map<String , Map<String,String>> userMap = new HashMap<>();

    @Resource
    private AuthMenuTreeVoController authMenuTreeVoController;

//    private AutoVoMapper

    private boolean inited;
    //用户认证
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //从数据库取出数据
        initUser(username);
        //获取指定用户名的用户信息
        Map<String, String> userInfo = userMap.get(username);

        //获取用户信息
        if (userInfo!=null) {
            return new User(username, userInfo.get("password"), AuthorityUtils.commaSeparatedStringToAuthorityList(userInfo.get("roleNameList")));
        }
        return new User(username,"",AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER"));
    }

    private void initUser(String username){

        AuthMenuTreeVo authVo = authMenuTreeVoController.getAuthMenuByUsername(username);
        Map<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("password", passwordEncoder.encode(authVo.getPassword()));
        hashMap.put("roleNameList", authVo.getRoleNameList().toString());
        userMap.put(authVo.getUserName(), hashMap);
//        RedisUtil.addToSet("userInfo", userMap);


       /* userMap = RedisUtil.getMembersOfMap("userInfo");

        if (RedisUtil.getMembersOfSet("userInfo")==null) {
            List<AuthMenuTreeVo> list = authMenuTreeVoController.getAuthMenuByUsername();
            if (userMap != null && list != null) {
                //遍历集合，将集合中数据整合到Map集合中
                for (AuthMenuTreeVo authMenuTreeVo : list) {
                    Map<String, String> hahsMap = new HashMap<String, String>();
                    hahsMap.put("password", passwordEncoder.encode(authMenuTreeVo.getPassword()));
                    hahsMap.put("roleNameList", authMenuTreeVo.getRoleNameList().toString());
                    userMap.put(authMenuTreeVo.getUserName(), hahsMap);
                }
            }
            RedisUtil.addToSet("userInfo", userMap);
        }*/

    }
}
